package com.charging.controller;


import cn.hutool.http.HttpUtil;
import com.charging.bean.ApiDiscount;
import com.charging.bean.Constent;
import com.charging.bean.Site;
import com.charging.bean.layuiBean.ResultPage;
import com.charging.bean.layuiBean.VoPage;
import com.charging.config.config.BaseController;
import com.charging.config.result.ApiResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-23
 */
@Controller
@RequestMapping("admin/apiDiscount")
public class ApiDiscountController extends BaseController {


    @RequestMapping(value = "discount.html",method = RequestMethod.GET)
    public String discount(){
        return "discount/discount";
    }

    @RequestMapping(value = "discount_edit.html",method = RequestMethod.GET)
    public String discount_edit(){
        return "discount/discount_edit";
    }


    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<ApiDiscount> selectMyPage(VoPage voPage){
        return apiDiscountService.selectMyPage(voPage);
    }


    /**
     * @param apiDiscount
     * @return 修改
     */
    @RequestMapping(value = "updateByIdDiscount",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateByIdDiscount(@RequestBody ApiDiscount apiDiscount){
        if(apiDiscountService.updateById(apiDiscount)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    /**
     * @param apiDiscount
     * @return 删除
     */
    @RequestMapping(value = "deleteByIdDiscount",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteByIdDiscount(@RequestBody ApiDiscount apiDiscount){
        if(apiDiscountService.removeById(apiDiscount)){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }


    /**
     * @param
     * @return 添加
     */
    @RequestMapping(value = "saveDiscount",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveDiscount(String ids,String discount){
        System.out.println("ids:"+ids);
        ApiDiscount apiDiscount = new ApiDiscount();
        String idss[] = ids.split(",");

        for (int i = 0; i < idss.length; i++) {
            String a[] = idss[i].split("@");
                    apiDiscount.setDiscount(discount);
                    apiDiscount.setId(null);
                    apiDiscount.setName(a[1]);
                    apiDiscount.setAddress(a[2]);
                    if(!apiDiscountService.save(apiDiscount)){
                        return ApiResult.fail();
            }
        }
        return ApiResult.ok();
    }


}
